package GUI.Gestion;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

public class dialog_Cajas extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private JTable tabla;
	private boolean editable;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the dialog.
	 * 
	 * @wbp.parser.constructor
	 */
	public dialog_Cajas(final JTable table, boolean edit) {
		this.editable = edit;
		this.tabla = table;
		this.inicilize();

	}

	public dialog_Cajas(final JTable table, int fila, boolean edit) {
		this.editable = edit;
		this.tabla = table;
		this.inicilize();
		dispose();

	}

	private void inicilize() {

		setBounds(100, 100, 193, 186);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[46px,center][95px]",
				"[][][]"));
		{
			JLabel lblId = new JLabel("ID");
			lblId.setHorizontalTextPosition(SwingConstants.CENTER);
			lblId.setHorizontalAlignment(SwingConstants.CENTER);
			contentPanel.add(lblId, "cell 0 0,alignx center,aligny center");
		}
		{
			textField_1 = new JTextField();
			contentPanel.add(textField_1,
					"cell 1 0,alignx center,aligny center");
			textField_1.setColumns(10);
		}
		{
			JLabel lblNombre = new JLabel("Numero");
			contentPanel.add(lblNombre, "cell 0 1,alignx center");
		}
		{
			textField = new JTextField();
			contentPanel.add(textField, "cell 1 1,alignx center,aligny center");
			textField.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						if (!editable) {
							Tabla modelo = new Tabla();
							modelo = (Tabla) tabla.getModel();

							String[] aux = new String[2];
							aux[0] = textField_1.getText();
							aux[1] = textField.getText();
							modelo.addRow(aux);
							
							tabla.setModel(modelo);
						} else {

							tabla.setValueAt(textField_1.getText(),
									tabla.getSelectedRow(), 0);
							tabla.setValueAt(textField.getText(),
									tabla.getSelectedRow(), 1);

						}
						dispose();

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
